//
//  CommentList.m
//  RRBI
//
//  Created by kuyuZJ on 16/7/5.
//
//

#import "CommentList.h"
#import "CommentListCell.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>

@interface CommentList ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property(nonatomic,strong) UITableView* tableView;
@property(nonatomic,strong) UIView* headerView;
@property(nonatomic,strong) UIView* topView;
@property(nonatomic,strong) UILabel* labelToal;
@property(nonatomic,strong) UILabel* labelFood;
@property(nonatomic,strong) UILabel* labelExpress;
@property(nonatomic,strong) UIView* bottomView;
@property(nonatomic,strong) UIButton* btnAll;
@property(nonatomic,strong) UIButton* btnBest;
@property(nonatomic,strong) UIButton* btnGood;
@property(nonatomic,strong) UIButton* btnBad;


@property(nonatomic,strong) NSMutableArray* arrayData;
@property(nonatomic,strong) NSArray* arrayBtn;
@property(nonatomic,strong) NSString* cellIdentifier;
/**
 *  type(评论类型，0：全部，1：差评，2：中评；3：好评)
 */
@property(nonatomic,strong) NSString* typeID;

@property(nonatomic,strong) NetPage* page;


@end

@implementation CommentList



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title =  @"用户评价";
    self.cellIdentifier = @"CommentListCell";
    self.typeID = @"0";
    
    [self layoutUI];
    [self layoutConstraints];
    [self refreshDataSource];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
         [self.btnAll setTitle:[NSString stringWithFormat:@"全部(%@)",@"0"] forState:UIControlStateNormal];
    [self.btnBest setTitle:[NSString stringWithFormat:@"好评(%@)",@"0"] forState:UIControlStateNormal];
    [self.btnGood setTitle:[NSString stringWithFormat:@"中评(%@)",@"0"] forState:UIControlStateNormal];
    [self.btnBad setTitle:[NSString stringWithFormat:@"差评(%@)",@"0"] forState:UIControlStateNormal];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark =====================================================  User interface layout
-(void)layoutUI{
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.tableView];
 
    [self.headerView addSubview:self.btnAll];
    [self.headerView addSubview:self.btnBest];
    [self.headerView addSubview:self.btnGood];
    [self.headerView addSubview:self.btnBad];
    
    self.arrayBtn = @[self.btnAll,self.btnBad,self.btnGood,self.btnBest];
    
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[CommentListCell class] forCellReuseIdentifier:self.cellIdentifier];
    
}

-(void)layoutConstraints{
    self.headerView.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnAll.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnBest.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnGood.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnBad.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.headerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.headerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.headerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.headerView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    CGFloat width = (SCREEN_WIDTH-50)/4;
    [self.btnAll addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAll attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAll attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAll attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnAll attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.f]];
    
    [self.btnBest addConstraint:[NSLayoutConstraint constraintWithItem:self.btnBest attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnBest attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnBest attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.btnAll attribute:NSLayoutAttributeRight multiplier:1.0 constant:10.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnBest attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.f]];
    
    [self.btnGood addConstraint:[NSLayoutConstraint constraintWithItem:self.btnGood attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnGood attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnGood attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.btnBest attribute:NSLayoutAttributeRight multiplier:1.0 constant:10.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnGood attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.f]];
    
    [self.btnBad addConstraint:[NSLayoutConstraint constraintWithItem:self.btnBad attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnBad attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnBad attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.btnGood attribute:NSLayoutAttributeRight multiplier:1.0 constant:10.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnBad attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.f]];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.f]];
    
}

#pragma mark =====================================================  Data Source
-(void)queryData{
    
    NSDictionary* arg = @{@"ince":@"get_shop_comment",@"sid":[UserModel sharedInstanced].ru_Id,@"type":self.typeID, @"page":[WMHelper integerConvertToString:self.page.pageIndex]};
       NetRepositories* repositories = [[NetRepositories alloc]init];     
    [repositories queryComment:arg page:self.page complete:^(EnumNetResponse react, NSArray *list, id model, NSString *message) {
        if(self.page.pageIndex == 1){
            [self.arrayData removeAllObjects];
        }
        if(react == NetResponseSuccess){
            [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.arrayData addObject:obj];
            }];
        }else if(react == NetResponseException){
             [self alertHUD:message];
        }else{
            [self alertHUD:message];
        }
        [self loadData:model];
        [self.tableView reloadData];
        if(self.page.pageCount<=self.page.pageIndex){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        if(self.page.pageIndex==1){
            [self.tableView.mj_header endRefreshing];
        }
        
    }];
    
    
}
-(void)refreshDataSource{
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page.pageIndex = 1;
        [weakSelf queryData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        weakSelf.page.pageIndex ++;
        [weakSelf queryData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark =====================================================  <UITableViewDataSource>
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayData.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 65.f;
    MComment* entity = self.arrayData[indexPath.row];
    CGFloat empty = [WMHelper calculateTextHeight:entity.comment font:[UIFont systemFontOfSize:14.f] width:SCREEN_WIDTH-20];
    return height+empty;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentListCell* cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    cell.entity = self.arrayData[indexPath.row];
    return cell;
}

#pragma mark =====================================================  <UITableViewDelegate>
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

#pragma mark =====================================================  DZEmptyData 协议实现
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    return [[NSAttributedString alloc] initWithString:@"暂无数据" attributes:@{NSFontAttributeName :[UIFont boldSystemFontOfSize:17.0],NSForegroundColorAttributeName:[UIColor grayColor]}];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return roundf(self.tableView.frame.size.height/10.0);
}
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

#pragma mark =====================================================  SEL
-(IBAction)changeTypeTouch:(UIButton*)sender{
    self.typeID  = [NSString stringWithFormat:@"%ld",(long)sender.tag];
    sender.selected = YES;
    for (NSInteger i = 0; i<(NSInteger)self.arrayBtn.count; i++) {
        if(i != sender.tag){
            UIButton* btn = (UIButton*)self.arrayBtn[i];
            btn.selected = NO;
        }
    }
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark =====================================================  private method
-(void)loadData:(MStoreComment *)item{
    if(item){
        [self.btnAll setTitle:[NSString stringWithFormat:@"全部(%@)",item.totalNum] forState:UIControlStateNormal];
        [self.btnBest setTitle:[NSString stringWithFormat:@"好评(%@)",item.bestNum] forState:UIControlStateNormal];
        [self.btnGood setTitle:[NSString stringWithFormat:@"中评(%@)",item.goodNum] forState:UIControlStateNormal];
        [self.btnBad setTitle:[NSString stringWithFormat:@"差评(%@)",item.badNum] forState:UIControlStateNormal];
    }
}

#pragma mark =====================================================  Property package

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
-(UIView *)headerView{
    if(!_headerView){
        _headerView = [[UIView alloc]init];
        _headerView.backgroundColor = [UIColor whiteColor];
        CALayer* border = [[CALayer alloc]init];
        border.frame = CGRectMake(0, 49.f, SCREEN_WIDTH, 1.f);
        border.backgroundColor = theme_line_color.CGColor;
        [_headerView.layer addSublayer:border];
    }
    return _headerView;
}


-(UIButton *)btnAll{
    if(!_btnAll){
        _btnAll  = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnAll.tag = 0;
        _btnAll.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_btnAll setBackgroundImage:[UIImage imageNamed:@"icon-comment-default"] forState:UIControlStateNormal];
        [_btnAll setBackgroundImage:[UIImage imageNamed:@"icon-comment-enter"] forState:UIControlStateSelected];
        [_btnAll setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_btnAll setTitleColor:[UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0] forState:UIControlStateNormal];
        _btnAll.selected = YES;
        [_btnAll addTarget:self action:@selector(changeTypeTouch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnAll;
}

-(UIButton *)btnBest{
    if(!_btnBest){
        _btnBest = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnBest.tag = 3;
        _btnBest.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_btnBest setBackgroundImage:[UIImage imageNamed:@"icon-comment-default"] forState:UIControlStateNormal];
        [_btnBest setBackgroundImage:[UIImage imageNamed:@"icon-comment-enter"] forState:UIControlStateSelected];
        [_btnBest setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_btnBest setTitleColor:[UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0] forState:UIControlStateNormal];
        [_btnBest addTarget:self action:@selector(changeTypeTouch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnBest;
}


-(UIButton *)btnGood{
    if(!_btnGood){
        _btnGood = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnGood.tag = 2;
        _btnGood.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_btnGood setBackgroundImage:[UIImage imageNamed:@"icon-comment-default"] forState:UIControlStateNormal];
        [_btnGood setBackgroundImage:[UIImage imageNamed:@"icon-comment-enter"] forState:UIControlStateSelected];
        [_btnGood setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_btnGood setTitleColor:[UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0] forState:UIControlStateNormal];
        [_btnGood addTarget:self action:@selector(changeTypeTouch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnGood;
}

-(UIButton *)btnBad{
    if(!_btnBad){
        _btnBad = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnBad.tag = 1;
        _btnBad.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_btnBad setBackgroundImage:[UIImage imageNamed:@"icon-comment-default"] forState:UIControlStateNormal];
        [_btnBad setBackgroundImage:[UIImage imageNamed:@"icon-comment-enter"] forState:UIControlStateSelected];
        [_btnBad setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_btnBad setTitleColor:[UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0] forState:UIControlStateNormal];
        [_btnBad addTarget:self action:@selector(changeTypeTouch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnBad;
}

-(NSMutableArray *)arrayData{
    if(!_arrayData){
        _arrayData = [[NSMutableArray alloc]init];
    }
    return _arrayData;
}

-(NetPage *)page{
    if(!_page){
        _page = [[NetPage alloc]init];
        _page.pageIndex = 1;
    }
    return _page;
}
@end
